package com.xl.minyue.tc2.service;

import com.alibaba.fastjson.JSON;
import com.xl.minyue.tc2.dao.ReportFormDao;
import com.xl.minyue.tc2.pojo.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ReportFormServiceImpl implements PiService{
    @Resource
    private ReportFormDao formDao;

    //pi实时数据

    //查询最新数据列表
    public List<TagInfo> getNewData(){
        return formDao.getNewData();
    }

    //更新实时数据的状态------taginfo表
    public boolean updateStatus(){
        return formDao.updateStatus();
    }

    //接收数据时把pointInfo表格里的中文名字直接写入到tagInfo表格中
    public String getPointNameToTagName2(String code){
        PointInfo info = formDao.getPointNameToTagName2(code);
        return info.getPointname();
    }

    //添加pointinfo
    public Boolean addPoints(PointInfo p) throws Exception {
        return formDao.addPoints(p);
    }

    //新修改的获取最新数据--------实时
    public List<TagInfo> showTagNewDataByKind_2(String kind1, String kind2) {
        List<PointOrder> orderList = formDao.checkPointOrderByKind(kind1, kind2);
        List<TagInfo> list = new ArrayList<>();
        SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd");
        String date = d.format(new Date());
        if (orderList.size()==0){
            List<PointInfo> pList = formDao.checkPointInfoByKind(kind1, kind2);
            System.out.println(pList.size()+"<--------pList长度1111111111111111");
            for (int i = 0; i < pList.size(); i++) {
                PointInfo p = pList.get(i);
                String tagName = p.getPointcode();
                int count = formDao.checkCountByCode(tagName, date);
                System.out.println(i+"-------"+tagName+"--------"+p.getPointname());
                if(count>0){
                    List<TagInfo> infoList2 = formDao.getDataByPointAndDate3(tagName, date);
                    if (infoList2.size()>0){
                        for (int j = 0; j <infoList2.size(); j++) {
                            System.out.println("");
                            TagInfo t = infoList2.get(j);
                            System.out.println(t.toString()+"-------------->"+i);
                            if(j==0){
                                t.setTagname2(p.getPointname());
                                list.add(t);
                            }
                        }
                    }
                }
            }
        }else{
            for (int i = 0; i < orderList.size(); i++) {
                PointOrder order = orderList.get(i);
                String tagName = order.getPointcode();
                int count = formDao.checkCountByCode(tagName, date);
                if(count>0){

                    List<TagInfo> infoList = formDao.getDataByPointAndDate3(tagName, date);
                    if (infoList.size()>0){
                        for (int j = 0; j <infoList.size(); j++) {

                            TagInfo t = infoList.get(j);
                            System.out.println(t.toString()+"-------------->"+i);
                            if(j==0){
                                t.setTagname2(order.getPointname());
                                list.add(t);
                            }
                        }
                    }

                   /* TagInfo info = formDao.getDataByPointAndDate(tagName, date);
                    //System.out.println(p.toString()+"<-----------"+i);
                    info.setTagname2(p.getPointname());
                    list.add(info);*/
                }
            }
        }
        return list;
    }

    public List<User> findAllUsers() throws Exception {
        return formDao.findAllUsers();
    }

    public Boolean delePoints(String id) throws Exception {
        return formDao.delePoints(id);
    }

    //根据点的分类查询点的信息2
    public List<PointInfo> checkPointInfoByKind2(String kind2,String kind1){
        return formDao.checkPointInfoByKind2(kind2, kind1);
    }

    //展示曲线数据
    public   List<TagInfo> showDataByDateAndPoint2(String tagName, String date, String nowTime) {
        return  formDao.getDataByPointAndDate2(tagName, date);
    }

    public   List<TagInfo> showDataByDateAndPoint2_(String tagName, String date) {
        List<TagInfo> list = new ArrayList<>();
        List<String> list2 = new ArrayList<>();
        List<TagInfo> list1 = formDao.getDataByPointAndDate2_(tagName, date);
        for (int i = 0; i < list1.size(); i++) {
            TagInfo t = list1.get(i);
            String colltime = t.getColltime();
            if (!list2.contains(colltime)){
                list2.add(colltime);
                list.add(t);
            }
        }
        return  list;
    }


    //日报
    //根据code(神华编码和note)查询点的信息
    public PointInfo checkInfoByNoteAndCode(String code, String note,String kind1) {
        return formDao.checkInfoByNoteAndCode(code,note,kind1);
    }

    //日报的最新数据展示
    public List<TagDataX3> showTagNewDataByKind2(String kind1, String kind2) {
        String yesterday = ReportFormServiceImpl.getYesterday();
        //yesterday="2018-08-20";
        return formDao.showTagNewDataByKind2(kind1,kind2, yesterday);
    }


    //新修改的获取最新数据---------日报
    public List<TagDataX3> showTagNewDataByKind2_NEW2(String kind1, String kind2) {
        List<PointOrder> orderList = formDao.checkPointOrderByKind(kind1, kind2);
        List<String> dateList=formDao.getDateList(kind1,kind2);
        System.out.println(JSON.toJSONString(dateList)+"<----------dateList");
        String yesterday = ReportFormServiceImpl.getYesterday();
        List<TagDataX3> list=new ArrayList<>();
        if (orderList.size()==0){
            List<TagDataX3> list3 = formDao.showTagNewDataByKind2(kind1, kind2, yesterday);
            if(list3.size()>0){
                list=list3;
            }else{
                for (int i = 0; i <dateList.size(); i++) {
                    String date=dateList.get(i);
                    System.out.println(date+"<-----"+i);
                    List<TagDataX3> list4 = formDao.showTagNewDataByKind2(kind1, kind2, date);
                    if(list4.size()>0){
                        list=list4;
                        break;
                    }
                }
            }
        }else{
            for (int i = 0; i <orderList.size(); i++) {
                PointOrder order = orderList.get(i);
                System.out.println(order.toString()+"---------PointOrder---------"+yesterday);
                String code = order.getPointcode();
                String note = order.getNote();
                List<TagDataX3> list2=null;
                list2=  formDao.findOneData(kind1,kind2,code,note,yesterday);
                System.out.println(list2+"list2_list2_list2_list2_list2_list2_"+order.getPointname());
                if(list2.size()>0){
                    for (int j = 0; j <list2.size(); j++) {
                        TagDataX3 t=list2.get(j);
                        System.out.println(t.toString()+"-------------->"+i);
                        if(j==0){
                            list.add(t);
                        }
                    }
                }else {
                    for (int k = 0; k <dateList.size(); k++) {
                        String date=dateList.get(k);
                        List<TagDataX3> list4 = formDao.findOneData(kind1,kind2,code,note, date);
                        if(list4.size()>0){
                            list2=list4;
                            break;
                        }
                    }
                    if(list2.size()>0){
                        for (int j = 0; j <list2.size(); j++) {
                            if(j==0){
                                list.add(list2.get(j));
                            }
                        }
                    }
                }
            }
        }
        return list;
    }


    public List<TagDataX3> showTagNewDataByKind2_NEW(String kind1, String kind2) {
        List<PointOrder> orderList = formDao.checkPointOrderByKind(kind1, kind2);
        System.out.println(JSON.toJSONString(orderList)+"<-orderList----showTagNewDataByKind2"+orderList.size());

        String yesterday = ReportFormServiceImpl.getYesterday();
        List<TagDataX3> list=new ArrayList<>();
        if (orderList.size()==0){
            list = formDao.showTagNewDataByKind2(kind1, kind2, yesterday);
        }else{
            for (int i = 0; i <orderList.size(); i++) {
                PointOrder order = orderList.get(i);
                String code = order.getPointcode();
                String note = order.getNote();
                List<TagDataX3> list2=  formDao.findOneData(kind1,kind2,code,note,yesterday);
                if(list2.size()>0){
                    for (int j = 0; j <list2.size(); j++) {
                        TagDataX3 t=list2.get(j);
                        if(j==0){
                            list.add(t);
                        }
                    }
                }
            }
        }
        return list;
    }

    //根据中文意思查询子表数据----------日报
    public List<TagDataX3> showDailyDataByName(String endDate, String startDate, String name) {
        List<TagDataX3> list = new ArrayList<>();
        List<String> dateList = new ArrayList<>();
        try {
            List<TagDataX3> dataX3List = formDao.showDailyDataByName(endDate, startDate, name);
            for (TagDataX3 t:dataX3List) {
                if(!dateList.contains(t.getDatetime2())){
                    dateList.add(t.getDatetime2());
                    list.add(t);
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return list;
    }

    //(1)插入日报---月报---数据
    public boolean addDataFromX3Daily(TagDataX3 t){
        return formDao.insertDataFromX3Daily(t);
    }

    public int addDataFromX3Daily2(TagDataX3 dataX3) {
        return formDao.insertDataFromX3Daily2(dataX3);
    }


    //月报
    public boolean deleTagDataX3(String kind1, String month) {
        return formDao.deleTagDataX3(kind1,month);
    }

    //月报最新数据展示
    public List<TagDataX3> showTagNewDataByMonth(String kind1, String kind2) {
        SimpleDateFormat dd = new SimpleDateFormat("dd");
        String month =null;
        int parseInt = Integer.parseInt(dd.format(new Date()));
        if(parseInt==1){
            month = ReportFormServiceImpl.getPreMonth();
        }else{
            SimpleDateFormat mm = new SimpleDateFormat("yyyy-MM");
            month = mm.format(new Date());
        }
        return formDao.showTagNewDataByKind2(kind1,kind2, month);
    }

    //新修改的获取最新数据---------月报
    public List<TagDataX3> showTagNewDataByMonth_New(String kind1, String kind2) {
        SimpleDateFormat dd = new SimpleDateFormat("dd");
        String month =null;
        int parseInt = Integer.parseInt(dd.format(new Date()));
        if(parseInt==1){
            month = ReportFormServiceImpl.getPreMonth();
        }else{
            SimpleDateFormat mm = new SimpleDateFormat("yyyy-MM");
            month = mm.format(new Date());
        }
        List<PointOrder> orderList = formDao.checkPointOrderByKind(kind1, kind2);
        //System.out.println(JSON.toJSONString(orderList)+"<-orderList----showNewDataByMonth---------"+orderList.size());
        List<TagDataX3> list2=new ArrayList<>();
        if (orderList.size()==0){
            System.out.println("");
            list2 = formDao.showTagNewDataByKind2(kind1,kind2, month);
        }else{
            for (int i = 0; i <orderList.size(); i++) {
                //System.out.println("");
                PointOrder order = orderList.get(i);
                String code = order.getPointcode();
                String note = order.getNote();
                System.out.println(order.toString()+"-----showNewDataByMonth----PointOrder");
                TagDataX3 tagDataX3=  formDao.findOneData2(kind1,kind2,code,note,month);
                /*List<TagDataX3> list3=  formDao.findOneData(kind1,kind2,code,note,month);
                for (int j = 0; j <list3.size(); j++) {

                    TagDataX3 t=list3.get(j);
                    System.out.println(t.toString()+"-------------->"+i);
                    if(j==0){
                        System.out.println(list3.size()+"<------------长度");
                        list2.add(t);
                    }
                }*/
                list2.add(tagDataX3);
            }
        }
        return list2;
    }

    //月报曲线展示
    public List<TagDataX3> showMonthDataByName(String startTime, String endTime, String name, String kind1) {
        try {
            return  formDao.showMonthDataByName(startTime,endTime,name,kind1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean addData(TagInfo info){
        return formDao.addData(info);
    }

    //获取昨天的日期
    public static String getYesterday(){
        String a="";
        SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = new Date();
        Calendar instance = Calendar.getInstance();
        instance.setTime(date1);
        instance.add(Calendar.DATE,-1);
        String format = d.format(instance.getTime());
        System.out.println(format+"<----------昨天");
        return format;
    }

    //获取上一个月的月份
    private static String getPreMonth() {
        Date date = new Date();
        SimpleDateFormat mm = new SimpleDateFormat("yyyy-MM");
        Calendar c= Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH,-1);
        String format = mm.format(c.getTime());
        System.out.println(format);
        return format;
    }

    //判断pointorder表格中在kind1和kind2下是否有数据
    public List<PointOrder> checkPointOrderByKind(String kind1, String kind2) {
        return formDao.checkPointOrderByKind(kind1,kind2);
    }

    public boolean updateOrAddOrderData(PointOrder p) {
        //System.out.println(p.toString()+"<----------顺序数据");
        //查询当前数据在PointOrder中存在不
        boolean up=false;
        boolean f=formDao.isExistOfPointOrder(p.getKind1(),p.getKind2(),p.getNote(),p.getPointcode());
        if(f){
            //修改
            System.out.println("修改Order!!!!");
            up=formDao.upOrder(p);
        }else{
            //添加
            System.out.println("添加Order!!!1");
            up=formDao.addOrder(p);
        }
        return up;
    }

    public List<String> gettimeList(String code, String day) throws ParseException {
        SimpleDateFormat d2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat d4 = new SimpleDateFormat("HH:mm");
        List<String> list2 = new ArrayList<>();
        List<TagInfo> list= formDao.gettimeList(code,day);
        //System.out.println(JSON.toJSONString(list)+"<-----------list----service----"+code+"---"+day);
       if(list.size()>0){
           for (int i = 0; i < list.size(); i++) {
               TagInfo t = list.get(i);
               Date time = d2.parse(t.getColltime());
               String date2 = d4.format(time);
               //System.out.println(!list2.contains(date2)+"<--"+i);
               if(!list2.contains(date2)){
                   list2.add(date2);
               }
           }
       }
       return list2;
    }

    public TagInfo showDataByDateAndPoint2_2(String tagName, String date, String colltime) {
        List<TagInfo> list= formDao.getDataByPointAndDate2_2(tagName, date,colltime);
        if (list.size()>0){
            for (int i = 0; i <list.size(); i++) {
                if (i==0){
                    return list.get(i);
                }
            }
        }
        return null;
    }




}
